<?php
require_once("Service.php");
require_once("Database.php");

class Account extends Service {
	
	var $conn;
	
	/** The constructor of this object. */
	function __construct() {
		
		parent::__construct();
		$this->conn = new Database();
	}
	
	/**
	 * Updates a user's account details on the system.
	 * @param $sessionID The session id of the user calling this function.
	 * @param $userID The id of the user.
	 * @param $firstName The first name of the user.
	 * @param $lastName The last name of the user.
	 * @param $email The email address of the user.
	 * @param $userName The user name of the user, "" to ignore.
	 * @param $password The password of the user, "" to ignore.
	 * @return true, if the opperation succeeded, otherwise false.
	 */
	function updateAccountDetails($sessionID, $userID, $firstName, $lastName, $email, $userName="", $password="") {
		
		$this->validateSession($sessionID);
		
		if($userName != "") {
			
			$result = $this->conn->execute("
				SELECT userID
				FROM Users 
				WHERE userName = '" . md5($userName) . "'
			");
			
			if(mysql_num_rows($result) != 0) {
				return false;
			}
		}
		$query = "
			UPDATE Users
			SET
				firstName = '" . $firstName . "',
				lastName = '" . $lastName . "',
				email = '" . $email . "'";
		$query .= ", userName = '" . md5($userName) . "'";
		$query .= ", password = '" . md5($password) . "'";
		$query .= "WHERE userID = '" . $userID . "'";
		$rowID = $this->conn->execute($query);
		
		return true;
	}
}
?>